package rwt.server.util;

import java.io.*;
import java.util.*;

import rwt.server.RailServer;
import rwt.server.core.*;
import rwt.server.core.User.UserRole;
import rwt.server.railroad.*;

public class JspUtil
{
	public static class ClientTrainInitData
	{
		public int trainId;
		public boolean isInitialized;
		public int headSensor;
		public int tailSensor;
	}
	
	public static String getRoleSelectBoxHtml(UserRole selectedRole)
	{
		StringBuilder sb = new StringBuilder(100);
		sb.append("\n\t\t\t<select name=\"role\">\n");
		for (UserRole role : UserRole.values())
		{
			String selText = "";
			if (role == selectedRole)
				selText = "selected=\"selected\" ";
			sb.append("\t\t\t\t<option " + selText + "value=\"" + role.getName() + "\">" + role.getName()
					+ "</option>\n");
		}
		sb.append("\t\t\t</select>\n\t\t");
		return sb.toString();
	}
	
	public static List<ClientTrainInitData> getTrainInitList()
	{
		List<Integer> sortedTrainIds = RailServer.getTrainManager().getSortedTrainIds();
		List<ClientTrainInitData> trainInitDataList = new ArrayList<>(sortedTrainIds.size());
		
		for (int trainId : sortedTrainIds)
		{
			Train train = RailServer.getTrainManager().getTrainById(trainId);
			ClientTrainInitData initData = new ClientTrainInitData();
			initData.trainId = train.getTrainId();
			initData.isInitialized = train.isInitialized();
			if (initData.isInitialized)
			{
				initData.headSensor = train.getInitHeadSensor();
				initData.tailSensor = train.getInitTailSensor();
			}
			else if (Config.trainDefaultPositionMap.containsKey(trainId))
			{
				// Load the default train position if it was specified in the config file
				Config.SensorPair sensors = Config.trainDefaultPositionMap.get(trainId);
				initData.headSensor = sensors.head;
				initData.tailSensor = sensors.tail;
			}
			
			trainInitDataList.add(initData);
		}
		
		return trainInitDataList;
	}
	
	public static class SwitchStateData
	{
		public int switchId;
		public String switchState;
		public int layoutX;
		public int layoutY;
	}
	
	// Get Switch states
	public static List<SwitchStateData> getSwitchStateList()
	{
		List<Integer> sortedSwitchIds = RailServer.getSwitchManager().getSortedSwitchIds();
		List<SwitchStateData> switchStateList = new ArrayList<>(sortedSwitchIds.size());
		
		try
		{
			Scanner layoutscanner = new Scanner(new File("data/switchlayout.dat"));
			
			for (int switchId : sortedSwitchIds)
			{
				SwitchStateData switchData = new SwitchStateData();
				SwitchStatus state = RailServer.getSwitchManager().getSwitchState(switchId);
				switchData.switchId = switchId;
				switchData.layoutX = layoutscanner.nextInt();
				switchData.layoutY = layoutscanner.nextInt();
				switch (state)
				{
				case BeginThrown:
				case BeginClosed:
					switchData.switchState = "changing";
					break;
				case Closed:
					switchData.switchState = "closed";
					break;
				case Thrown:
					switchData.switchState = "thrown";
					break;
				case Unknown:
				default:
					switchData.switchState = "unknown";
					break;
				}
				switchStateList.add(switchData);
			}
			
			layoutscanner.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		return switchStateList;
	}
}
